package com.spring.gop.proj.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.MemberAddService;
import com.spring.gop.proj.interfaces.SearchService;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/member/*")
@SessionAttributes({"id", "name", "age","member"})
public class MemberController {

	@Autowired private MemberAddService memberAddService;
	@Autowired private SearchService searchService;
		
	@RequestMapping(value="/join1", method=RequestMethod.GET)
	public void join1Form() {}
	
	@RequestMapping(value="/join1", method=RequestMethod.POST)
	public String join1() {		
		return "redirect:join2";
	}
	
	@RequestMapping(value="/join2", method=RequestMethod.GET)
	public void join2Form() {}
	
	@RequestMapping(value="/join2", method=RequestMethod.POST)
	public String join2(@ModelAttribute("addUser")User user, HttpSession session, Model model) {
		
		String birth = user.getYear() +"/"+ user.getMon() +"/"+ user.getDay();
		String phone = user.getHeader() +"-"+ user.getCenter() +"-"+ user.getTail();
		String email = user.getEheader() + "@" + user.getEtail();
		
		User add = memberAddService.memberAdd(user.getMember_id(), user.getMember_name(), user.getMember_pw(), email, phone, birth);
		
		model.addAttribute("name", user.getMember_name());
		model.addAttribute("id", user.getMember_id());
	
		
		return "redirect:join3";
		}

	@RequestMapping(value="/join3", method=RequestMethod.GET)
	public void join3Form() {}

	@RequestMapping(value="/join3", method=RequestMethod.POST)
	public void join3() {}
	
	@RequestMapping(value="/check", method=RequestMethod.GET)
	public void checkForm(){}
	
	@RequestMapping(value="/check", method=RequestMethod.POST)
	public String check(@ModelAttribute("checkUser")User newUser,HttpSession session, Model model){
		
		User checkId = searchService.checkId(newUser.getMember_id());
		
		if(checkId == null){		
			session.setAttribute("message", "사용가능한 아이디입니다."); 
			session.setAttribute("newUser", newUser);
			session.setMaxInactiveInterval(30);
			return "redirect:checkid ";
			
		}else session.setAttribute("message", "이미 가입된 아이디입니다.");
		session.setAttribute("newUser", newUser);
		session.setMaxInactiveInterval(30);
		return "redirect:checkid";
	}
	
	@RequestMapping(value="/checkid", method=RequestMethod.GET)
	public void checkid(){}

	
}
	